<?php

$sql = "SELECT * FROM sys_atomic_configuration LIMIT 1";
$configData = $db->mysqlDB->query($sql);

//判断是否有配置信息
if(count($configData)<=0){
   $response->responseData(false,"没有找到相关配置信息");
}

//创建相关配置变量
$ac_is_debug                         = setIntConversionBool($configData[0]['ac_is_debug']);
$ac_is_independent_deployment        = setIntConversionBool($configData[0]['ac_is_independent_deployment']);
$ac_is_physically_delete             = setIntConversionBool($configData[0]['ac_is_physically_delete']);
$ac_is_access_control_allow_origin   = setIntConversionBool($configData[0]['ac_is_access_control_allow_origin']);
$ac_is_aggregation_server_validation = setIntConversionBool($configData[0]['ac_is_aggregation_server_validation']);
$ac_is_web_manager_add_api 			 = setIntConversionBool($configData[0]['ac_is_web_manager_add_api']);
$ac_is_data_filter 			 = setIntConversionBool($configData[0]['ac_is_data_filter']);

$ac_method = "";
$methodArr = explode(",",$configData[0]['ac_method']);
for($i=0;$i<count($methodArr);$i++){
    $ac_method.= "'".$methodArr[$i]."',";
}
$ac_method = rtrim($ac_method,",");


$ac_router_layer = $configData[0]['ac_router_layer'];
$ac_built_in_url = $configData[0]['ac_built_in_url'];
$ac_is_curd                        = setIntConversionBool($configData[0]['ac_is_curd']);
$ac_page_limit                     = $configData[0]['ac_page_limit'];
$ac_is_white_auth                  = setIntConversionBool($configData[0]['ac_is_white_auth']);
$ac_is_cache                       = setIntConversionBool($configData[0]['ac_is_cache']);
$ac_cache_type                     = $configData[0]['ac_cache_type'];
$ac_cache_dir                      = $configData[0]['ac_cache_dir'];
$ac_cache_validity                 = $configData[0]['ac_cache_validity'];
$ac_noDatabaseUse                  = setIntConversionBool($configData[0]['ac_noDatabaseUse']);
$ac_mysql_username                 = $configData[0]['ac_mysql_username'];
$ac_mysql_password                 = $configData[0]['ac_mysql_password'];
$ac_mysql_port                     = $configData[0]['ac_mysql_port'];
$ac_mysql_host                     = $configData[0]['ac_mysql_host'];
$ac_mysql_dbname                   = $configData[0]['ac_mysql_dbname'];

$appConfig = <<<EOT
<?php
/*
 * 应用基本配置数组
 */
\$GLOBALS['settings'] = array(
    
    //设置调试模式
    "debug"	=> $ac_is_debug,
    
    //接口相关日志信息
	"interfaceInfo" => array(),

	//是否为独立部署(无聚合层)
	"isIndependentDeployment" => $ac_is_independent_deployment,

	//路由解析层级设置（支持几级路由）
	"routerLayerNumber" => $ac_router_layer,

	//是否开启数据过滤
	"isDataFilter" => $ac_is_data_filter,

	//内置接口项目访问地址
  "projectIp"	=> '$ac_built_in_url',

	//是否使用管理端创建API接口
	"isWebManagerAddApi" => $ac_is_web_manager_add_api,

	//是否需要聚合层验权
	"isAggregationServerValidation" => $ac_is_aggregation_server_validation,

	//是否开启物理删除
	"isPhysicallyDelete" => $ac_is_physically_delete,

	//是否开启跨域请求
	"isAccessControlAllowOrigin" => $ac_is_access_control_allow_origin,

	//设置允许的请求模式
	"method" => array($ac_method),

	//是否允许自动构建标准CURD
	"isCURD" => $ac_is_curd,

	//默认查询时分页显示数量
	"pageLimit" => $ac_page_limit,

	//是否开启接口白名单验证
	"isWhiteAuth" => $ac_is_white_auth,

	//缓存相关配置
	"cache" => array(

		//是否进行缓存
		"isCache" => $ac_is_cache,

		//缓存的类型 file:文件缓存
		"cacheType" => '$ac_cache_type',

		//缓存的目录 根目录/目录名称
		"cacheDir" => '$ac_cache_dir',
		
		//缓存的过期时间(秒)
		"cacheValidity" => $ac_cache_validity

    ),
    
    //是否需要接入数据库
	"noDatabaseUse" => $ac_noDatabaseUse,

	//数据库相关配置，将所有出现在配置项中的数据库进行连接以及操作对象的创建
	"database" => array(

		//Mysql相关配置项
		"Mysql" => array(
			"username"    => '$ac_mysql_username',
			"password"    => '$ac_mysql_password',
			"port"        => $ac_mysql_port,
			"host"        => '$ac_mysql_host',
			"dbname"   	  => '$ac_mysql_dbname'
		)
    )
    
);
?>        
EOT;

//判断该目录是否有写入权限
if(!is_writable('./config/appConfig.php')){
	$response->responseData(false,"配置文件目录无写入权限");
}

if(file_put_contents('./config/appConfig.php',$appConfig)){
	$response->responseData(true);
}else{
	$response->responseData(false,"配置信息更新失败");
}




